﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Tnelab.Wpf
{
    public partial class WindowEx
    {
		public static readonly DependencyProperty IsTitleBarProperty =
			DependencyProperty.RegisterAttached("IsTitleBar", typeof(bool), typeof(WindowEx), new(false, OnIsTitleBarChanged));
		static void OnIsTitleBarChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{

			var titleBarElement= obj as UIElement;
			if (e.NewValue != e.OldValue)
			{
				var value = (bool)e.NewValue;
                if (value)
                {
                    titleBarElement.MouseLeftButtonDown += TitleBarElement_MouseLeftButtonDown;
                }
                else
                {
					titleBarElement.MouseLeftButtonDown -= TitleBarElement_MouseLeftButtonDown;
				}
			}
		}

        private static void TitleBarElement_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
			var window = Window.GetWindow(sender as DependencyObject);
			window.DragMove();
		}

        public static bool GetIsTitleBar(UIElement dpo)
		{
			return (bool)dpo.GetValue(IsTitleBarProperty);
		}

		public static void SetIsTitleBar(UIElement dpo, bool value)
		{
			dpo.SetValue(IsTitleBarProperty, value);
		}
	}
}
